package com.zzm.validate.web.injector;

import org.apache.commons.validator.Arg;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.Form;

import com.zzm.validate.web.FormValidatorStub;

public abstract class ArgInjector {
	
	private String injectKey;
	
	protected FormValidatorStub formValidatorStub;
	
	public FormValidatorStub getFormValidatorStub() {
		return formValidatorStub;
	}

	public void setFormValidatorStub(FormValidatorStub formValidatorStub) {
		this.formValidatorStub = formValidatorStub;
	}

	public String getInjectKey() {
		return injectKey;
	}

	public void setInjectKey(String injectKey) {
		this.injectKey = injectKey;
	}

	public boolean injectable(Arg arg) {
		return this.getInjectKey().equalsIgnoreCase(arg.getKey());
	}
	
	public abstract String inject(Object bean, Form form, Field field, Arg arg, String message);
	
	protected String inject(String message, Arg arg, String value) {
		return message.replace(formValidatorStub.getArgPattern().indexOf(arg.getPosition()), value);
	}
}
